package cn.mxb.logic.action.mxb;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.entity.SysZu;
import cn.mxb.logic.entity.MxbPrsinfo;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysUserService;
import cn.mxb.logic.service.ISysVillageService;
import cn.mxb.logic.service.ISysZuService;
import unit.IdGenerator;
import unit.MyTime;
import cn.mxb.logic.service.IMxbPrsinfoService;

@SuppressWarnings({ "serial", "rawtypes" })
public class MxbPrsinfoAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/prsinfo/prsinfoList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/prsinfo/prsinfoAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/prsinfo/prsinfoUpdate.jsp";
	
	
	private MxbPrsinfo mxbPrsinfo;
	@Autowired
	private IMxbPrsinfoService mxbPrsinfoService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysZuService sysZuService;
	@Autowired
	private ISysUserService sysUserService;
	
	private Result<MxbPrsinfo> pageResult;
	
	private List<MxbPrsinfo> mxbPrsinfoAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	private List<SysZu> sysZuAllList; 
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();    //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();     //镇名
	private Map<Integer,String> villageNameMap = new HashMap<Integer, String>();  //村名
	private Map<Integer,String> zUidMap = new HashMap<Integer, String>(); 
	private Map<String,String> prsinfoNameMap= new HashMap<String, String>(); 
	private Map<String,String> prsinfoPhoneMap= new HashMap<String, String>(); 
	private Map<String,Integer> prsinfoSexMap= new HashMap<String, Integer>(); 
	
	private String familyNameY; //参合家庭姓名搜索关键字
	private String userRealNameY; //报销人名字查询
	private String idcarId;
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		sysZuAllList = sysZuService.findAllList();
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @throws Exception 
	 * @return_type: String
	 */
	public String add() throws Exception{
		System.out.println("add");
		/*IdGenerator id = new IdGenerator(1023);
		mxbPrsinfo.setFamilyId((id.getNextId()).intValue());*/
		
		return openList();		
	}
	
	/**
	 * 参合(ginseng)登记
	 * @return
	 */
	public String ginsengAdd(){
		try{ System.out.println("ginsengAdd");
			mxbPrsinfo.setJoinYear(MyTime.getNowDate()+"");
			System.out.println(MyTime.getNowDate()+"");
			//根据当前年度参合
			mxbPrsinfoService.add(mxbPrsinfo);
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		setForwardView(List_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			if(idcarId !=null &&!"".equals(idcarId)){
				mxbPrsinfo.setPrsinfoId(sysUserService.findByProperty("cardId", idcarId).getUserId());
			}
			pageResult = mxbPrsinfoService.find(mxbPrsinfo, getPage(), getRow());
			if(pageResult.getData().size() == 0){
				setForwardView(null);
			}
			for(MxbPrsinfo mxbPrsinfo : pageResult.getData()){
				if(null != mxbPrsinfo.getPrsinfoId()  && !"".equals(mxbPrsinfo.getPrsinfoId())){
					prsinfoNameMap.put(mxbPrsinfo.getPrsinfoId(),sysUserService.findByProperty("userId", mxbPrsinfo.getPrsinfoId()).getUserRealName());
					prsinfoSexMap.put(mxbPrsinfo.getPrsinfoId(),sysUserService.findByProperty("userId", mxbPrsinfo.getPrsinfoId()).getUserSex());
					prsinfoPhoneMap.put(mxbPrsinfo.getPrsinfoId(),sysUserService.findByProperty("userId", mxbPrsinfo.getPrsinfoId()).getUserPhone()+"");
				}
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		mxbPrsinfoService.delete(mxbPrsinfo.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		mxbPrsinfo =  mxbPrsinfoService.findByProperty("id",mxbPrsinfo.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		mxbPrsinfoService.add(mxbPrsinfo);
		mxbPrsinfo = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return mxbPrsinfo;
	}
	@Override
	public void prepare() throws Exception {
		if(mxbPrsinfo ==null){
			mxbPrsinfo = new MxbPrsinfo();
		}
	}


	public MxbPrsinfo getMxbPrsinfo() {
		return mxbPrsinfo;
	}

	public void setMxbPrsinfo(MxbPrsinfo mxbPrsinfo) {
		this.mxbPrsinfo = mxbPrsinfo;
	}

	public Result<MxbPrsinfo> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<MxbPrsinfo> pageResult) {
		this.pageResult = pageResult;
	}
	
	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	public Map<Integer, String> getVillageNameMap() {
		return villageNameMap;
	}

	public void setVillageNameMap(Map<Integer, String> villageNameMap) {
		this.villageNameMap = villageNameMap;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public List<MxbPrsinfo> getMxbPrsinfoAllList() {
		return mxbPrsinfoAllList;
	}

	public void setMxbPrsinfoAllList(List<MxbPrsinfo> mxbPrsinfoAllList) {
		this.mxbPrsinfoAllList = mxbPrsinfoAllList;
	}

	public List<SysZu> getSysZuAllList() {
		return sysZuAllList;
	}

	public void setSysZuAllList(List<SysZu> sysZuAllList) {
		this.sysZuAllList = sysZuAllList;
	}

	public Map<Integer, String> getzUidMap() {
		return zUidMap;
	}

	public void setzUidMap(Map<Integer, String> zUidMap) {
		this.zUidMap = zUidMap;
	}

	public String getFamilyNameY() {
		return familyNameY;
	}

	public void setFamilyNameY(String familyNameY) {
		this.familyNameY = familyNameY;
	}

	public Map<String, String> getPrsinfoNameMap() {
		return prsinfoNameMap;
	}

	public void setPrsinfoNameMap(Map<String, String> prsinfoNameMap) {
		this.prsinfoNameMap = prsinfoNameMap;
	}

	

	public Map<String, String> getPrsinfoPhoneMap() {
		return prsinfoPhoneMap;
	}

	public void setPrsinfoPhoneMap(Map<String, String> prsinfoPhoneMap) {
		this.prsinfoPhoneMap = prsinfoPhoneMap;
	}

	public Map<String, Integer> getPrsinfoSexMap() {
		return prsinfoSexMap;
	}

	public void setPrsinfoSexMap(Map<String, Integer> prsinfoSexMap) {
		this.prsinfoSexMap = prsinfoSexMap;
	}

	public String getUserRealNameY() {
		return userRealNameY;
	}

	public void setUserRealNameY(String userRealNameY) {
		this.userRealNameY = userRealNameY;
	}

	public String getIdcarId() {
		return idcarId;
	}

	public void setIdcarId(String idcarId) {
		this.idcarId = idcarId;
	}

	


	

	
	
}
